/**
 * Project Name:SimonFramework
 * File Name:ImageLoaderBiz.java
 * Package Name:com.simon.framework.image
 * Date:2016-5-19 上午11:31:29
 * Copyright (c) 2016, simon@cmonbaby.com All Rights Reserved.
 */
package com.cmonbaby.images;

import android.widget.ImageView;

import com.cmonbaby.images.core.DisplayImageOptions;
import com.cmonbaby.images.core.ImageLoader;
import com.cmonbaby.images.core.listener.ImageLoadingListener;
import com.cmonbaby.images.core.listener.ImageLoadingProgressListener;

/**
 * <p>Author:      Simon
 * <p>QO:          8950764
 * <p>Email:       simon@cmonbaby.com
 * <p>WebSize:     https://www.cmonbaby.com
 * <p>Version:     1.0.0
 * <p>Date:        2020/12/29
 * <p>Description: 加载本地图片的工具类
 */
public class ImageLoaderBiz {

    private ImageLoader imageLoader;
    private ImageView imageView;
    private String loadUrl;
    private int drawableUrl;
    private int resEmpty; // 设置图片Uri为空或是错误的时候显示的图片
    private int resFail; // 设置图片加载或解码过程中发生错误显示的图片
    private DisplayImageOptions options; // 图片的所有配置
    private ImageLoadingListener listener; // 图片加载监听
    private ImageLoadingProgressListener progressListener; // 图片加载进度监听

    public static Builder with() {
        return new Builder();
    }

    private ImageLoaderBiz(Builder builder) {
        imageLoader = ImageLoader.getInstance();
        this.imageView = builder.imageView;
        this.loadUrl = builder.loadUrl;
        this.drawableUrl = builder.drawableUrl;
        this.resEmpty = builder.resEmpty;
        this.resFail = builder.resFail;
        this.options = builder.options;
        this.listener = builder.listener;
        this.progressListener = builder.progressListener;
        startWork();
    }

    /**
     * <ul>
     * <li>loadUrl.contains("drawable://") // 从从drawable中异步加载本地图片</li>
     * <li>loadUrl.contains("file://") // 从内存卡中异步加载本地图片</li>
     * <li>loadUrl.contains("assets://") // 从assets文件夹中异步加载图片</li>
     * <li>loadUrl.contains("content://") // 从内容提提供者中抓取图片</li>
     * </ul>
     */
    private void startWork() {
        if (imageView == null) return;
        if (drawableUrl != 0) { // 从drawable中异步加载本地图片
            loadUrl = "drawable://" + drawableUrl;
        }

        if (resEmpty != 0 || resFail != 0) {
            options = ImageLoaderConfig.initDisplayOptions(resEmpty, resFail);
        }
        imageLoader.displayImage(loadUrl, imageView, options, listener, progressListener);
    }

    public static class Builder {

        private ImageView imageView;
        private String loadUrl;
        private int drawableUrl;
        private int resEmpty; // 设置图片Uri为空或是错误的时候显示的图片
        private int resFail; // 设置图片加载或解码过程中发生错误显示的图片
        private DisplayImageOptions options; // 图片的所有配置
        private ImageLoadingListener listener; // 图片加载监听
        private ImageLoadingProgressListener progressListener; // 图片加载进度监听

        public Builder() {
        }

        public Builder from(String loadUrl) {
            this.loadUrl = loadUrl;
            return this;
        }

        public Builder from(int drawableUrl) {
            this.drawableUrl = drawableUrl;
            return this;
        }

        public Builder into(ImageView imageView) {
            this.imageView = imageView;
            return this;
        }

        public Builder resEmpty(int resEmpty) {
            this.resEmpty = resEmpty;
            return this;
        }

        public Builder resFail(int resFail) {
            this.resFail = resFail;
            return this;
        }

        public Builder options(DisplayImageOptions options) {
            this.options = options;
            return this;
        }

        public Builder listener(ImageLoadingListener listener) {
            this.listener = listener;
            return this;
        }

        public Builder progressListener(ImageLoadingProgressListener progressListener) {
            this.progressListener = progressListener;
            return this;
        }

        public ImageLoaderBiz display() {
            return new ImageLoaderBiz(this);
        }
    }
}